/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.admin.rest;

import static com.automvc.common.service.CommService.hasNull;
import static com.automvc.common.service.CommService.isNullPara;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.BeeSQLException;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;

import com.aiteasoft.school.admin.entity.Assigncourse;
import com.aiteasoft.school.admin.service.AssigncourseService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-02-07 18:21:08
 */
@RestController
@RequestMapping("assigncourse")
public class AssigncourseRest {
	@Autowired
	ObjSQLService objSQLService;
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	AssigncourseService assigncourseService;

	@RequestMapping("/list")
	public Result list(Assigncourse assigncourse, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

		Result result = new Result();
		if (isNullPara(result, assigncourse)) return result;
		
		assigncourse = assigncourseService.process(assigncourse);
		PageWarp pageWarp = assigncourseService.countAndSelect(assigncourse, (page - 1) * rows, rows);
		result.setTotal(pageWarp.getTotal());
		result.setRows(pageWarp.getList());

		return result;
	}
	
	@RequestMapping("/add")
	public Result insert(Assigncourse assigncourse) {

		Result result = new Result();
		if (isNullPara(result, assigncourse)) return result;

		String checkFields = "classno,term,teacherno,subjectno,";
		if (hasNull(result, assigncourse, checkFields)) return result;

		int num = objSQLService.insert(assigncourse);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("insert failed!");
		return result;
	}

	@RequestMapping("/edit")
	public Result update(Assigncourse assigncourse) {

		Result result = new Result();
		if (isNullPara(result, assigncourse)) return result;
		if(assigncourseService.isWrongBizForUpdate(result, assigncourse)) return result;

		String checkFields = "id,classno,term,teacherno,subjectno,";
		if (hasNull(result, assigncourse, checkFields)) return result;
		
		assigncourse = assigncourseService.process(assigncourse);
		
		int num = assigncourseService.update(assigncourse);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("update failed!");
		return result;
	}
	
	@PostMapping("/assign")
//	public Result addArray(@RequestBody ArrayObject arrayObject) {  //ok
//		public Result addArray(@RequestBody Assigncourse[] assigncourses) {//ok
		public Result addArray(@RequestBody Assigncourse assigncourses[]) {//ok
//		public Result addArray(Assigncourse assigncourses[]) { //不行
//		public Result addArray(@RequestBody Assigncourse assigncourses[]) {
		Result result = new Result();
		try {
//			Assigncourse[] assigncourses=arrayObject.getAssigncourses();
			if (isNullPara(result, assigncourses, "assigncourses")) return result;
			
			String checkFields = "classno,term,teacherno,subjectno,";
			for (int i = 0; i < assigncourses.length; i++) {
				if (hasNull(result, assigncourses[i], checkFields)) return result;
				assigncourses[i].setStatus("1");
			}
			
			int a=objSQLRichService.insert(assigncourses);
			result.setTotal(a);
			if(a==assigncourses.length)
			   result.setMsg("全部插入!");
		} catch (BeeSQLException e) {
			e.printStackTrace();
			result.setErrorMsg(e.getMessage());
		}

		return result;
	}

}